﻿using System.Xml.Linq;
using NJabber.Stream.Elements;
using NUnit.Framework;

namespace NJabber.Stream.Elements
{
    [TestFixture]
    public class StreamHeaderTest : DatagramElementTestBase
    {
        public StreamHeaderTest() : base("stream", XmppNamespaces.Streams, "stream")
        {
        }

        [Test]
        public void Write_ClientHeaderWithDeclaration_WritesCorrectXml()
        {
            VerifyStreamHeaderXml(StreamType.Client, XmppNamespaces.JabberClient, true);
        }

        [Test]
        public void Write_ServerHeaderWithDeclaration_WritesCorrectXml()
        {
            VerifyStreamHeaderXml(StreamType.Server, XmppNamespaces.JabberServer, true);
        }

        [Test]
        public void Write_ClientHeaderWithoutDeclaration_WritesCorrectXml()
        {
            VerifyStreamHeaderXml(StreamType.Client, XmppNamespaces.JabberClient, false);
        }

        [Test]
        public void Write_ServerHeaderWithoutDeclaration_WritesCorrectXml()
        {
            VerifyStreamHeaderXml(StreamType.Server, XmppNamespaces.JabberServer, false);
        }

        private void VerifyStreamHeaderXml(StreamType streamType, string streamNamespace, bool includeDeclaration)
        {
            StreamHeader header = new StreamHeader(streamType, FromJid, ToJid, Id);
            string expectedXml = CreateStreamHeaderXmlString(streamNamespace, FromJid, ToJid, Id, includeDeclaration);
            header.WriteXmlDeclaration = includeDeclaration;
            VerifyWriteOutput(expectedXml, header);
        }

        private string CreateStreamHeaderXmlString(string streamNamespace, JID from, JID to, object id, bool includeDeclaration)
        {
            XDeclaration declaration = new XDeclaration("1.0", null, null);
            XElement header = XmlUtils.CreateStreamHeader(streamNamespace, from, to, id);
            string xmlString = includeDeclaration ? declaration.ToString() : "";
            xmlString += header.ToString();
            return xmlString;
        }
    }
}
